package com.graphsql.gamesdk.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtils {

	public static <T> T bindData(Object object, Class<T> cls) {
		return ObjectMapper.bindModel(object.toString(), cls);
	}

	public static <T> List<T> bindDataList(Object object, Class<T> cls) {
		return ObjectMapper.bindModelList(object.toString(), cls);
	}

	public static Map bindDataToModel(String content) {
		return ObjectMapper.bindDataToMap(content);
	}
	
	public static String getValue(String json, String key) {
		if (json == null || key == null) {
			return null;
		}
		if (!(json.startsWith("{") && json.endsWith("}"))) {
			return null;
		}
		try {
			JSONObject mjson = new JSONObject(json);
			Iterator it = mjson.keys();
			while (it.hasNext()) {
				String next = it.next().toString();
				if (next.equalsIgnoreCase(key)) {
					return mjson.get(next).toString();
				}
			}
		} catch (JSONException e) {
			LogDebugger.exception(e.getMessage());
		}
		return null;
	}

}
